<!DOCTYPE html> 
<html> 
  <head>  
    <!--META-->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=0.75">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">

    <!--CSS-->
    <link rel="stylesheet" href="API/css/themes/default/jquery.mobile.min.css" />
    <link rel="stylesheet" href="API/css/themes/default/jquery.mobile.structure.min.css" />
    <link rel="stylesheet" href="API/css/themes/default/jquery.mobile.theme.min.css" />
    <link rel="stylesheet" href="API/css/mycss.css" />
    <link rel="stylesheet" href="css/mycss.css" />

    <!--JS-->
    <script type="text/javascript" src="API/js/jquery.min.js"></script>
    <script type="text/javascript" src="API/js/jquery.mobile.min.js"></script>
    <script type="text/javascript" src="API/js/highcharts.js"></script>
    <script type="text/javascript" src="API/js/modules/exporting.js"></script>
    <!--<script type="text/javascript" src="API/js/jquery.dataTables.min.js"></script>--> 
    <script type="text/javascript" src="include/config.js"></script>
    <script type="text/javascript" src="API/js/fonctions.js"></script>
    <script type="text/javascript" src="js/fonctions.js"></script>

    <script language="javascript" type="text/javascript">
    checkMaintenance();
    checkAuthentification();
	
		///////////////////
		//BLOCK VARIABLE GLOBAL
		///////////////////
		var id_page = 17;
		
	
		///////////////////
		//BLOCK FONCTIONS EVENEMENTS
		///////////////////
		$(document).on('pageinit',function() {
		    try {
				// Init commmun
				init_commun();
			
				// Init page
            }
            catch (er) {
                log(0, "ERROR(init_home):" + er.message);
            }
        });

		$(document).on('pageshow',function() {
			try {
				show_commun();
			}
			catch (er) {
				log(0, "ERROR(show):" + er.message);
			}
		});
		
		///////////////////
		//BLOCK FONCTIONS METIER
		///////////////////
		function AfficherDetailTickets(){
			try {
                var v_listeDefect = getInputDetailTickets();
				
				var tabDetailTickets = new Array();
				tabDetailTickets = getDetailTickets(v_listeDefect);
				
				strhtml = "";
				strhtml += "<center>";
				strhtml += "<table data-role=\"table\" data-mode=\"reflow\" class=\"ui-body-d ui-shadow table-stripe ui-responsive\">";
				strhtml += "<thead>";
				strhtml += "<TR class=\"ui-bar-d\">";
				strhtml += "<TH>Defect</TH>";
				strhtml += "<TH>Nombre</TH>";
				strhtml += "<TH>Tickets</TH>";
				strhtml += "</thead>";
				strhtml += "<tbody>";
				id_defect_courant = "";
				for (var indice in tabDetailTickets) {
					if(id_defect_courant != tabDetailTickets[indice][0]){
						
						if(id_defect_courant != ""){
							strhtml += "</TD>";
							strhtml += "</TR>";
						}
						
						id_defect_courant = tabDetailTickets[indice][0];
						
						strhtml += "<TR onMouseOver=\"changeCouleur(this);\" onMouseOut=\"remetCouleur(this);\" >";
						strhtml += "<TD>"+tabDetailTickets[indice][0]+"</TD>";
						strhtml += "<TD>"+tabDetailTickets[indice][1]+"</TD>";
						strhtml += "<TD><a target=\"_new\" href=\"http://10.197.68.30:4000/AutoDiseWeb/automate/popupModule.jsp?modType=SIG_SIG&modCode=" + tabDetailTickets[indice][2] + "\">" + tabDetailTickets[indice][2] + "</a>("+tabDetailTickets[indice][3]+")<br>";
					}else{
						strhtml += "<a target=\"_new\" href=\"http://10.197.68.30:4000/AutoDiseWeb/automate/popupModule.jsp?modType=SIG_SIG&modCode=" + tabDetailTickets[indice][2] + "\">" + tabDetailTickets[indice][2] + "</a>("+tabDetailTickets[indice][3]+")<br>";
					}		
				}
				strhtml += "</TD>";
				strhtml += "</TR>";
				strhtml += "</tbody>";
				strhtml += "</table>";
				strhtml += "</center>";
			
				$("#divDetailTickets").html(strhtml).trigger('create');
				
				$("#content_DetailTickets").trigger( "expand" );
				$( "#content_conso" ).trigger( "collapse" );
            }
            catch (er) {
                log(0, "ERROR(AfficherDetailTickets):" + er.message);
            }
		}
		
		function getUrlCsvDetailTickets(){
			try {
				var v_listeDefect = getInputDetailTickets();
				
				var url = "http://10.85.32.5/ODA_DISE/phpsql/mysql_getDetailTicketsDefect.php?milis="+getMilise()+"&filterDefect=" + v_listeDefect + "&csv=oui&filename=ExportCsvDetailTickets";
				
				return url;
            }
            catch (er) {
                log(0, "ERROR(getUrlCsvTab):" + er.message);
				return "";
            }
		}
		
		function getInputDetailTickets(){
			try {
                var p_dateBegin = $("#dateDepart").val();
				var p_dateEnd = $("#dateFin").val();
				var p_filterDefect = $("#filterDefect").val();
				
                if (p_dateBegin == "") {
                    p_dateBegin = "01/01/1970";
                }
                if (p_dateEnd == "") {
                    p_dateEnd = "01/01/2070";
                }
				
				var reg = new RegExp("[/]", "g");
                var strDateBegin = p_dateBegin.replace(reg, '');
                var strDateEnd = p_dateEnd.replace(reg, '');
				
				if(p_filterDefect == ""){
					var tabDefect = new Array();
					tabDefect = getTabDataPhase(strDateBegin,strDateEnd,p_filterDefect);
					
					var strListeDefect = "D0";

					for (var indice in tabDefect) {
						strListeDefect += ','+tabDefect[indice][0];
					}
				}else{
					strListeDefect = p_filterDefect;
				}
				
				return strListeDefect;
            }
            catch (er) {
                log(0, "ERROR(getInputDetailTickets):" + er.message);
				return null;
            }
		}
		
		function getUrlCsvTab(){
			try {
				var p_dateBegin = $("#dateDepart").val();
				var p_dateEnd = $("#dateFin").val();
				var p_filterDefect = $("#filterDefect").val();
				
                if (p_dateBegin == "") {
                    p_dateBegin = "01/01/1970";
                }
                if (p_dateEnd == "") {
                    p_dateEnd = "01/01/2070";
                }
				
				var reg = new RegExp("[/]", "g");
                var strDateBegin = p_dateBegin.replace(reg, '');
                var strDateEnd = p_dateEnd.replace(reg, '');
				
				var url = "phpsql/mysql_getRappDefectChargeCsv.php?milis="+getMilise()+"&dateBegin=" + strDateBegin+"&dateEnd=" + strDateEnd+"&filterDefect=" + p_filterDefect + "&csv=oui&filename=ExportCsvRapportDefects";
				
				return url;
            }
            catch (er) {
                log(0, "ERROR(getUrlCsvTab):" + er.message);
				return "";
            }
		}
		
		function AfficherResultat(p_dateBegin, p_dateEnd){
			try {
                if (p_dateBegin == "") {
                    p_dateBegin = "01/01/1970";
                }
                if (p_dateEnd == "") {
                    p_dateEnd = "01/01/2070";
                }
				
				var reg = new RegExp("[/]", "g");
                var strDateBegin = p_dateBegin.replace(reg, '');
                var strDateEnd = p_dateEnd.replace(reg, '');
				
				var p_filterDefect = $("#filterDefect").val();
				
				$( "#content_conso" ).trigger( "expand" );
				$( "#content_DetailTickets" ).trigger( "collapse" );
				
				chargeDefect(strDateBegin,strDateEnd,p_filterDefect);
            }
            catch (er) {
                log(0, "ERROR(AfficherResultat):" + er.message);
            }
		}
		
		function chargeDefect(p_dateBegin,p_dateEnd,p_filterDefect) {
            try {
                var tabPhase = new Array();
				tabPhase = getTabDataPhase(p_dateBegin,p_dateEnd,p_filterDefect);
				
				var tabCat = new Array();

				for (var indice in tabPhase) {
				    tabCat[tabCat.length] = tabPhase[indice][0];
				}
				
				var tabCharge = new Array();
				tabCharge = getTabDataCharge(p_dateBegin,p_dateEnd,p_filterDefect);
				
				var tabSeries = new Array();
                
				var phaseCourrante = "";
				var tabSubSeries = new Array();
				
				for (var indice in tabCharge) {
					if(phaseCourrante != tabCharge[indice][3]){
						if(phaseCourrante != ""){
							tabSeries[tabSeries.length] = {
								name: phaseCourrante,
								data: tabSubSeries
							};
							
							var tabSubSeries = new Array();
						}
					
					
						phaseCourrante = tabCharge[indice][3];
					}
					
					tabSubSeries[tabSubSeries.length] = parseInt(tabCharge[indice][2]);
				}
				
				tabSeries[tabSeries.length] = {
					name: phaseCourrante,
					data: tabSubSeries
				};
				
                afficheDefect(tabCat, tabSeries);
            }
            catch (er) {
                log(0, "ERROR(chargeDefect):" + er.message);
            }
        }
		
		///////////////////
		//BLOCK FONCTIONS BD
		///////////////////
		function getDetailTickets(p_filterDefect) {
            try {
                var strResponse = "";
                var returns = new Array();
                var tempTab = new Array();
                var tempSubTab = new Array();

                xhr_object = new AJ();

                var url = "http://10.85.32.5/ODA_DISE/phpsql/mysql_getDetailTicketsDefect.php?milis="+getMilise()+"&filterDefect=" + p_filterDefect;

                xhr_object.open("GET", url, false);
                xhr_object.send(null);
                if (xhr_object.readyState == 4) {
                    strResponse = xhr_object.responseText;
                } else {
                    strResponse = "ERROR";
                }

                tempTab = strResponse.split("\n");

                for (var indice in tempTab) {
                    tempSubTab = tempTab[indice].split("&");
                    if (tempSubTab.length > 1) {
                        returns[returns.length] = tempSubTab.slice(0, tempSubTab.length - 1);
                    }
                }

                delete xhr_object;

                return returns;
            }
            catch (er) {
                log(0, "ERROR(getDetailTickets):" + er.message);
            }
        }
		
		function getTabDataPhase(p_dateBegin, p_dateEnd, p_filterDefect) {
            try {
                var strResponse = "";
                var returns = new Array();
                var tempTab = new Array();
                var tempSubTab = new Array();

                xhr_object = new AJ();

                var url = "phpsql/mysql_getRappDefectPhase.php?milis="+getMilise()+"&dateBegin=" + p_dateBegin+"&dateEnd=" + p_dateEnd+"&filterDefect=" + p_filterDefect;

                xhr_object.open("GET", url, false);
                xhr_object.send(null);
                if (xhr_object.readyState == 4) {
                    strResponse = xhr_object.responseText;
                } else {
                    strResponse = "ERROR";
                }

                tempTab = strResponse.split("\n");

                for (var indice in tempTab) {
                    tempSubTab = tempTab[indice].split("&");
                    if (tempSubTab.length > 1) {
                        returns[returns.length] = tempSubTab.slice(0, tempSubTab.length - 1);
                    }
                }

                delete xhr_object;

                return returns;
            }
            catch (er) {
                log(0, "ERROR(getTabDataPhase):" + er.message);
            }
        }
		
		function getTabDataCharge(p_dateBegin, p_dateEnd, p_filterDefect) {
            try {
                var strResponse = "";
                var returns = new Array();
                var tempTab = new Array();
                var tempSubTab = new Array();

                xhr_object = new AJ();

                var url = "phpsql/mysql_getRappDefectCharge.php?milis="+getMilise()+"&dateBegin=" + p_dateBegin+"&dateEnd=" + p_dateEnd+"&filterDefect=" + p_filterDefect;

                xhr_object.open("GET", url, false);
                xhr_object.send(null);
                if (xhr_object.readyState == 4) {
                    strResponse = xhr_object.responseText;
                } else {
                    strResponse = "ERROR";
                }

                tempTab = strResponse.split("\n");

                for (var indice in tempTab) {
                    tempSubTab = tempTab[indice].split("&");
                    if (tempSubTab.length > 1) {
                        returns[returns.length] = tempSubTab.slice(0, tempSubTab.length - 1);
                    }
                }

                delete xhr_object;

                return returns;
            }
            catch (er) {
                log(0, "ERROR(getTabDataCharge):" + er.message);
            }
        }
		
		
		///////////////////
		//BLOCK FONCTIONS AFFICHAGE
		///////////////////
		
		function afficheDefect(p_tabCat, p_tabSeries) {
			try {
				var chart;

				$(document).ready(function() {
					chart = new Highcharts.Chart({
						exporting: {
							url: 'http://export.highcharts.com',
							filename: 'defect'
						},
						chart: {
							renderTo: 'divTab',
							type: 'column'
						},
						title: {
							text: 'Consomm\351 par Defect'
						},
						xAxis: {
							categories: p_tabCat
						},
						yAxis: {
							min: 0,
							title: {
								text: 'Consomm\351 en dixi\350me '
							},
							stackLabels: {
								enabled: true,
								style: {
									fontWeight: 'bold',
									color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
								}
							}
						},
						legend: {
							backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
							borderColor: '#CCC',
							borderWidth: 1,
							shadow: true
						},
						tooltip: {
							formatter: function() {
								return '<b>'+ this.x +'</b><br/>'+
									this.series.name +': '+ this.y +'<br/>'+
									'Total: '+ this.point.stackTotal;
							}
						},
						plotOptions: {
							column: {
								stacking: 'normal',
								dataLabels: {
									enabled: true,
									color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
								}
							}
						},
						series: p_tabSeries
					});
				});
			}
            catch (er) {
                log(0, "ERROR(afficheDefect):" + er.message);
            }
        }	
		
    </script>

  </head>
  <body>
    
	<!-- page -->
	<div data-role="page" data-title="Titre">
	
		<!-- /panel -->
		<div data-role="panel" id="mypanel" data-display="overlay" data-position="left">

		</div>
		<!-- /panel -->
	
		<!-- header -->
		<div data-role="header" data-position="fixed">
			<a href="#mypanel" data-role="button" data-icon="home" data-iconpos="notext">home</a>
			<h1 id="id_titre">titre</h1>
			<a href="javascript:window.location = ('./page_contact.html?mili='+getMilise());" data-role="button" data-icon="info" data-iconpos="notext">Contact</a>
		</div>
		<!-- /header -->

		<!-- content -->
		<div data-role="content" id="main_content">
		
			<div data-role="collapsible" data-collapsed="false" data-mini="true">
				<h3>Ma recherche</h3>
				<FORM method="post" id="formTab" action="#">
					Analyse pour la p&eacute;riode de 
					<a href="javascript:inputMoisEnCours('dateDepart','dateFin');" data-role="button" data-inline="true" data-mini="true" title="Mois en cours">M</a>
					<a href="javascript:inputSemaineEnCours('dateDepart','dateFin');" data-role="button" data-inline="true" data-mini="true" title="Semaine en cours">S</a>
					<a href="javascript:inputDeuxSemaines('dateDepart','dateFin');" data-role="button" data-inline="true" data-mini="true" title="Deux derni&egrave;res semaines">S,S-1</a>
					:
					<input type="text" name="dateDepart" id="dateDepart" data-mini="true" placeholder="(DD/MM/YYYY)" />
					<input type="text" name="dateFin" id="dateFin" data-mini="true" placeholder="(DD/MM/YYYY)" />
					<input type="text" name="filterDefect" id="filterDefect" data-mini="true" placeholder="vide ou D001 ou par ex D001,D042" />
					
					<a href="javascript:AfficherResultat(document.forms.formTab.dateDepart.value, document.forms.formTab.dateFin.value);" data-role="button" data-inline="true" data-icon="arrow-r" data-mini="true">Afficher le consomé</a>
					<a href="javascript:window.open(getUrlCsvTab());" id="lien_export_tab" data-role="button" data-inline="true" data-icon="arrow-r" data-mini="true">Export CSV consomé</a>
					
					<a href="javascript:AfficherDetailTickets();" data-role="button" data-inline="true" data-icon="arrow-r" data-mini="true">Tickets par Defect</a>
					<a href="javascript:window.open(getUrlCsvDetailTickets());" id="lien_export_DetailTickets" data-role="button" data-inline="true" data-icon="arrow-r" data-mini="true">Export CSV Tickets par Defect</a>
				</FORM>
			</div>
			
			<div data-role="collapsible" data-collapsed="true" data-mini="true" id="content_conso">
				<h3>Le Consomm&eacute; par Defect</h3>
				<div id="divTab">Pas de r&eacute;sultat.</div>
			</div>
			
			<div data-role="collapsible" data-collapsed="true" data-mini="true" id="content_DetailTickets">
				<h3>Le d&eacute;tail en Tickets</h3>
				<div id="divDetailTickets">Pas de r&eacute;sultat.</div>
			</div>
			
		</div>
		<!-- /content -->
		
		<!-- footer -->
		<div data-role="footer" data-position="fixed">
			<a data-role="button" data-icon="search" data-iconpos="notext" class="ui-btn-left" href="javascript:window.location = ('./page_faq.html?mili='+getMilise());">FAQ</a>
			<h1 id="id_affichageUser">User</h1>
			<a data-role="button" data-icon="delete" data-iconpos="notext" class="ui-btn-right" href="javascript:window.location = ('./page_sortie.html?mili='+getMilise());">Logout</a>
		</div>
		<!-- /footer -->
		
	</div>
	<!-- /page -->
  </body>
</html>